package cn.appsys.service.developer;

import cn.appsys.dao.appinfo.AppInfoMapper;
import cn.appsys.dao.appversion.AppVersionMapper;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class AppInfoServiceImpl implements AppInfoService {
    @Resource
    private AppInfoMapper appInfoMapper;
    @Resource
    private AppVersionMapper appVersionMapper;

    @Override
    public boolean add(AppInfo appInfo) throws Exception {
        boolean flag = false;
        if(appInfoMapper.add(appInfo) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean modify(AppInfo appInfo) throws Exception {
        boolean flag = false;
        if(appInfoMapper.modify(appInfo) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteAppInfoById(Integer delId) throws Exception {
        return false;
    }

    @Override
    public List<AppInfo> getAppInfoList(String querySoftwareName,
                                        Integer queryStatus,
                                        Integer queryCategoryLevel1,
                                        Integer queryCategoryLevel2,
                                        Integer queryCategoryLevel3,
                                        Integer queryFlatformId,
                                        Integer devId,
                                        Integer currentPageNo,
                                        Integer pageSize) throws Exception {
        return appInfoMapper.getAppInfoList(querySoftwareName, queryStatus, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, queryFlatformId, devId, (currentPageNo-1)*pageSize, pageSize);
    }

    @Override
    public int getAppInfoCount(String querySoftwareName,
                               Integer queryStatus,
                               Integer queryCategoryLevel1,
                               Integer queryCategoryLevel2,
                               Integer queryCategoryLevel3,
                               Integer queryFlatformId,
                               Integer devId) throws Exception {
        return appInfoMapper.getAppInfoCount(querySoftwareName,queryStatus,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3,queryFlatformId,devId);
    }

    @Override
    public AppInfo getAppInfo(Integer id, String APKName) throws Exception {
        return appInfoMapper.getAppInfo(id, APKName);
    }

    @Override
    public boolean deleteAppLogo(Integer id) throws Exception {
        boolean flag = false;
        if(appInfoMapper.deleteAppLogo(id) > 0){
            flag = true;
        }
        return flag;
    }

    //业务：根据APPId删除app信息
    // 1.通过appid查询app_version这个表中是否有关联的数据
    // 2.若版本表中有该app应用对应的版本信息，则进行删除，先删除的是版本表中的信息，再删除app基本信息
    // 3.若版本表中无该app应用对应的版本信息，则直接删除app基本信息
    // 注意：事务控制
    @Override
    public boolean appsysdeleteAppById(Integer id) throws Exception {
        boolean flag = false;
        int versionCount = appVersionMapper.getVersionCountByAppId(id);//判断该app下有没有历史版本
        List<AppVersion> appVersionList = null;
        if(versionCount > 0){
            //首先删除版本信息
            appVersionList = appVersionMapper.getAppVersionList(id);
            for (AppVersion appVersion:appVersionList){
                if(appVersion.getApkLocPath() != null && !("").equals(appVersion.getApkLocPath())){
                    File file = new File(appVersion.getApkLocPath());
                    if(file.exists()){
                        if(!file.delete()){
                            throw new Exception();
                        }
                    }
                }
            }
            appVersionMapper.deleteVersionByAppId(id);
        }
        //删除APP基础信息
        //删除上传的logo图片
        AppInfo appInfo = appInfoMapper.getAppInfo(id,null);
        if (appInfo.getLogoLocPath() != null && !("").equals(appInfo.getLogoLocPath())) {
            File file = new File(appInfo.getLogoLocPath());
            if(file.exists()){
                if(!file.delete()){
                    throw new Exception();
                }
            }
        }
        //删除app_info表中的数据
        if(appInfoMapper.deleteAppInfoById(id) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean appsysUpdateSaleStatusByAppId(AppInfo appInfoObj) throws Exception {
        AppInfo appInfo = appInfoMapper.getAppInfo(appInfoObj.getId(),null);
        Integer operator = appInfoObj.getModifyBy();
        if(operator < 0 || appInfoObj.getId() < 0){
            throw new Exception();
        }
        if(null == appInfo){
            return false;
        }else {
            switch (appInfo.getStatus()){
                case 2://当状态为审核通过，可以进行上架操作
                    onSale(appInfo,operator,4,2);
                    break;
                case 5://当状态为已下架，可以进行上架操作
                    onSale(appInfo,operator,4,2);
                    break;
                case 4://当状态为已上架，可以进行下架操作
                    offSale(appInfo,operator,5);
                    break;
                default:
                    return false;
            }
        }
        return true;
    }

    private void onSale(AppInfo appInfo,Integer operator,Integer appInfStatus,Integer versionStatus) throws Exception{
        offSale(appInfo,operator,appInfStatus);
        setSaleSwitchToAppVersion(appInfo,operator,versionStatus);
    }


    private boolean offSale(AppInfo appInfo,Integer operator,Integer appInfStatus) throws Exception{
        AppInfo _appInfo = new AppInfo();
        _appInfo.setId(appInfo.getId());
        _appInfo.setStatus(appInfStatus);
        _appInfo.setModifyBy(operator);
        _appInfo.setOffSaleDate(new Date(System.currentTimeMillis()));
        appInfoMapper.modify(_appInfo);
        return true;
    }

    private boolean setSaleSwitchToAppVersion(AppInfo appInfo,Integer operator,Integer saleStatus) throws Exception{
        AppVersion appVersion = new AppVersion();
        appVersion.setId(appInfo.getVersionId());
        appVersion.setPublishStatus(saleStatus);
        appVersion.setModifyBy(operator);
        appVersion.setModifyDate(new Date(System.currentTimeMillis()));
        appVersionMapper.modify(appVersion);
        return false;
    }
}
